package servlet;


import util.JDBCUtil;
import model.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;


@WebServlet(urlPatterns = "/SearchUserServlet2.do")
public class search extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response){
        List<User> userList = new ArrayList<>();
        JDBCUtil jdbcUtil = new JDBCUtil();
        String username = request.getParameter("username");

        try {
            String sql = "SELECT * FROM 02_38_yangyang WHERE username=?";
            ResultSet resultSet = jdbcUtil.prepareQuery(sql, username);

            while (resultSet.next()) {
                User user = new User();
                user.setId(resultSet.getInt("user_id"));
                user.setUsername(resultSet.getString("username"));
                user.setGender(resultSet.getString("gender"));
                user.setProfession(resultSet.getString("profession"));
                user.setFavor(resultSet.getString("favor"));
                user.setDescription(resultSet.getString("description"));
                user.setRole(resultSet.getInt("role"));
                user.setState(resultSet.getInt("state"));

                userList.add(user);
            }


            request.setAttribute("userList", userList);
            request.getRequestDispatcher("usermanager/searchUser.jsp").forward(request, response);

        } catch (SQLException ex) {

        } catch (ServletException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            jdbcUtil.closeConnection(); // Close JDBC resources properly in a finally block
        }
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<User> userList = new ArrayList<>();
        JDBCUtil jdbcUtil = new JDBCUtil();
        String username = request.getParameter("username");

        try {
            String sql = "SELECT * FROM 02_38_yangyang WHERE username=?";
            ResultSet resultSet = jdbcUtil.prepareQuery(sql, username);

            while (resultSet.next()) {
                User user = new User();
                user.setId(resultSet.getInt("user_id"));
                user.setUsername(resultSet.getString("username"));
                user.setGender(resultSet.getString("gender"));
                user.setProfession(resultSet.getString("profession"));
                user.setFavor(resultSet.getString("favor"));
                user.setDescription(resultSet.getString("description"));
                user.setRole(resultSet.getInt("role"));
                user.setState(resultSet.getInt("state"));

                userList.add(user);
            }

            request.setAttribute("userList", userList);
            request.getRequestDispatcher("/searchUser.jsp").forward(request, response);

        } catch (SQLException ex) {
            throw new ServletException("Database access error", ex);
        } finally {
            jdbcUtil.closeConnection(); // Close JDBC resources properly in a finally block
        }
    }
}
